package it.polimi.swim.servlet;

import it.polimi.swim.entity.HelpRequest;
import it.polimi.swim.entity.Skill;
import it.polimi.swim.entity.SystemUser;
import it.polimi.swim.session.FriendManagerBeanRemote;
import it.polimi.swim.session.HelpRequestManagerBean;
import it.polimi.swim.session.HelpRequestManagerBeanRemote;
import it.polimi.swim.session.SkillManagerBean;
import it.polimi.swim.session.SkillManagerBeanRemote;
import it.polimi.swim.session.SystemUserManagerBeanRemote;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HelpRequestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Context ctx;

	public HelpRequestServlet() {
		super();

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String description;
		int idCreator, idSkill;

		try {
			System.out.println("post");
			
			Properties env = new Properties();
			env.put(Context.INITIAL_CONTEXT_FACTORY,
					"org.jnp.interfaces.NamingContextFactory");
			env.put(Context.PROVIDER_URL, "localhost:1099");
			InitialContext jndiContext = new InitialContext(env);

			System.out.println("Chiamo il sesison");
			ctx = new InitialContext();
			Object ref = ctx.lookup(HelpRequestManagerBean.REMOTE);
			HelpRequestManagerBeanRemote helpRequestManager = (HelpRequestManagerBeanRemote) ref;
			Object ref2 = ctx.lookup(SkillManagerBean.REMOTE);
			SkillManagerBeanRemote skillManager = (SkillManagerBeanRemote) ref2;
			Object ref3 = ctx.lookup("FriendManagerBean/remote");
			FriendManagerBeanRemote friendManager = (FriendManagerBeanRemote) ref3;
			
			

			//idCreator from session
			HttpSession session = request.getSession();
			idCreator = (Integer) session.getAttribute("id");
			
			// TODO RECUPERA INFORMAZIONI HELPREQUEST
			idSkill=Integer.parseInt(request.getParameter("skill"));

			description=request.getParameter("description");
			String title = request.getParameter("title");
			String helperSet = request.getParameter("helperSet");
			
			Skill s = skillManager.getSkillById(idSkill);
			HelpRequest hr = helpRequestManager.createHelpRequest(title, description, s, idCreator);
			
			
			List<SystemUser> user = s.getUsers();
			if(user==null) System.out.println("null!");
			if(helperSet.compareTo("friendsBySkill")==0){
				List<SystemUser> userSkill=new ArrayList();
				List<SystemUser> friends = friendManager.showMyFriendshipRequest(idCreator);
				for(SystemUser u: user){
					if(friends.contains(u)){
						userSkill.add(u);
					}
				}
				user = userSkill;
			}
			request.setAttribute("userList", user);

			request.getRequestDispatcher("/showPossibleUsers.jsp?idHelp="+hr.getId()).forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
